from flask import Blueprinit, request, Response, jsonify, render_template,redirect
import json
from models import User
from settings import db
from models import House

user_page = Blueprinit('user_page',__name__)
@user_page.route('user-page',__name__)
@user_page.route('/user/<name>')
def register():
    name = request.form['usename']
    password = request.form['password']
    email = request.form['email']
    result = User.query.filter_by()
    if len(result) == 0:

       user = User(name=name, password=password,email=email)
       db.session.add(user)
       json_str = json.dumps({'vaild': '1', 'msg': user.name})
       res = Response(json_str)
       res.set_cookie('name', user.name, 3600 * 2)
       return res
    else:
       return jsonify({'vaild':'0','msg':'用户已注册！'})
def user(name):
    user = User.guery.filter(User.name == name).first()
    if user:
       collect_id_str = user.collect_id
       if collect_id_str:
           collect_id_list = collect_id_str.split(',')
           collect_house_list = []
           for hid in collect_id_list:
               house = House.query.get(int(hid))
               collect_house_list.append(house)
    else:
        collect_house_list = []
    seen_id_str = user.seen_id
    if seen_id_str:
        seen_id_list = seen_id_str.split(',')
        seen_house_list = []
        for hid in seen_id_list:
            house = House.query.get(int(hid))
            seen_house_list.append(house)
    else:
        seen_house_list = []
        return render_template('user_page.html', user=user,
                               collect_house_list=seen_house_list)





